﻿/* Project: Side Scrolling Shooter
 * The explosion caused by an enemy's death.
 * Uses similar as bomb explosion.
 * Last Update: 4/12/11,11:16PM
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TheFifthSpace
{
    public class EffectExplosion: DrawableGameComponent
    {
        public float posX, posY; // Current x and y positions
        public Texture2D sprite;
        //private SpriteBatch spriteBatch;
        private Vector2 offset;
        private const int x = 320;
        private const int y = 280;
        private const int fps_Max = 20;
        public Point currentFrame;
        private int frameStart;
        int updateCount;
        public bool IsAlive { get; set; }

        public Vector2 Position
        {
            set
            {
                posX = value.X;
                posY = value.Y;
            }
            get
            {
                return new Vector2(posX, posY);
            }
        }

        public EffectExplosion(Game g, Vector2 p)
            : base(g)
        {
            Position = p;
            currentFrame = new Point(0, 0);
            frameStart = 0;
            updateCount = 0;
            IsAlive = true;
        }

        // LOAD CONTENT
        protected override void LoadContent()
        {
            sprite = MainGame.bombExplosionSprite;
            //spriteBatch = MainGame.spriteBatch;
            offset = new Vector2(x / 2, y / 2);
        }

        // DRAW
        public override void Draw(GameTime gameTime)
        {
            // Make sure animation fps is no faster than game fps
            frameStart += gameTime.ElapsedGameTime.Milliseconds;
            if (frameStart >= fps_Max) frameStart -= fps_Max;

            MainGame.spriteBatch.Begin();
            MainGame.spriteBatch.Draw(sprite, Position - offset,
                new Rectangle(currentFrame.X * 320, currentFrame.Y * 240, x, y), Color.White);
            MainGame.spriteBatch.End();
        }

        public override void Update(GameTime gameTime)
        {
            updateCount++;
            if (updateCount > 12)
            {
                currentFrame.X++; // move the source rectangle around spritesheet
                if (currentFrame.X >= 4)
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y >= 5)
                        IsAlive = false;
                }
                updateCount = 0;
            }
        }
    }
}
